package com.xj.hk.service.impl;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.xj.hk.dao.CkDao;
import com.xj.hk.entity.Ck;
import com.xj.hk.service.CkService;
import com.xj.util.Page;
/**
 * 成果转化服务实现类
 * @author AutoExport
 *
 */
@Component
@Transactional
public class CkServiceImpl implements CkService {

	@Autowired
	private CkDao ckDao;
	
	public void setCkDao(CkDao ckDao) {
        this.ckDao = ckDao;
    }


	@Override
	public Page<Object[]> queryByPage(Map<String, Object> map, int pageNo,int pageSize,String orderBy,String order) {
		String sql="select * from ck c left join sp s on c.sid=s.id order by c.id";
		String sqlCount="select count(c.id) from ck c left join sp s on c.sid=s.id";
		List<Object[]> result = ckDao.findPageDataObject(map,sql, pageNo, pageSize);
 	 	List<Object[]> count = ckDao.findPageDataObject(map,sqlCount, -1, -1);
 		Page<Object[]> page = new Page<Object[]>();
 		page.pageNo(pageNo).pageSize(pageSize).autoCount(true);
 		// 将结果写入分页Bean
 		page.setResult(result);
 		Object o = count.get(0);
 		// 设置总记录数
 		page.setTotalCount(Long.parseLong(o.toString()));
		return page;
	}


	@Override
	public void save(Ck vo) {
		ckDao.saveOrUpdate(vo);
		ckDao.flush();
	}


	@Override
	public void update(int id,int sid) {
		String hql="update Ck set sid=? where id=?";
		Query query  = ckDao.getSession().createQuery(hql).setParameter(0,sid).setParameter(1,id);
		query.executeUpdate();
		ckDao.flush();
		
	}
	
}
